const express = require('express')
const app = express()
const bodyParser = require('body-parser')
// 处理静态资源
app.use(express.static('public'))
// 处理参数
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// 设置允许跨域访问该服务
app.all('*', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
  res.header("Access-Control-Allow-Headers", "X-Requested-With,Content-Type,mytoken");
  next();
});
// 测试接口1
app.get('/getdata1', (req, res) => {
  setTimeout(function(){
    res.send('{"success":true,"message":"getdata1获取数据成功"}')
  },2000)
})
// 测试接口1
app.get('/getdata2', (req, res) => {
  res.send('{"success":true,"message":"getdata2获取数据成功"}')
})

app.get('/a1', (req, res) => {
  res.send(`
  <body>
    <div id="app">
      <h1>我是界面1</h1>
      <a href="/a2">点击跳转到界面2</a>
    </div>
  </body>
  `)
})
app.get('/a2', (req, res) => {
  res.send(`
  <body>
    <div id="app">
      <h1>我是界面2</h1>
      <h1>我是界面2</h1>
      <h1>我是界面2</h1>
    </div>
  </body>
  `)
})
app.get('/a3', (req, res) => {
  res.send('{"success":true,"message":"a3获取数据成功"}')
})

// 启动监听
app.listen(3001, () => {
  console.log('running...')
})